package com.lym.order.controller;

import com.lym.order.bean.Order;
import com.lym.order.properties.OrderProperties;
import com.lym.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 订单控制器
 */
//@RefreshScope // 激活配置属性的自动刷新功能
@RestController
public class OrderController {

    @Autowired
    OrderService orderService;

    //@Value("${order.timeout}")
    //String orderTimeout;

    //@Value("${order.auto-confirm}")
    //String orderAutoConfirm;

    @Autowired
    OrderProperties orderProperties;

    /**
     * 创建订单
     *
     * 调用路径: http://localhost:8000/order/create?userId=2&productId=1001
     *
     * @return
     */
    @GetMapping("/order/create")
    public Order createOrder(@RequestParam("productId") Long productId, @RequestParam("userId") Long userId) {
        Order order = orderService.createOrder(productId, userId);
        return order;
    }

    /**
     * 秒杀订单
     *
     * 调用路径: http://localhost:8000/order/create?userId=2&productId=1001
     *
     * @return
     */
    @GetMapping("/order/seckill")
    public Order seckillOrder(@RequestParam("productId") Long productId, @RequestParam("userId") Long userId) {
        Order order = orderService.createOrder(productId, userId);
        order.setId(Long.MAX_VALUE); // 模拟订单id为最大值
        return order;
    }

    /**
     * 获取订单配置信息
     *
     * 调用路径: http://localhost:8000/order/config
     *
     * @return
     */
    @GetMapping("/order/config")
    public String config() {
        //return  "order.timeout:" + orderTimeout + "; order.auto-confirm:" + orderAutoConfirm;
        return  "order.timeout:" + orderProperties.getTimeout() + "; " +
                "order.auto-confirm:" + orderProperties.getAutoConfirm() + "; " +
                "order.db-url:" + orderProperties.getDbUrl();
    }

}
